﻿using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.Infrastructure.WPF.Interfaces;
using SD.Infrastructure.WPF.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Client.ViewModels.StockOutOrder
{
    /// <summary>
    /// 
    /// </summary>
    public class SelectStockOutOrderViewModel : ScreenBase, IPaginatable
    {
        #region # 字段及构造器
        /// <summary>
        /// 库存单据服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IStockOrderContract> _iStockOrderContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public SelectStockOutOrderViewModel(ServiceProxy<IStockOrderContract> iStockOrderContract)
        {
            _iStockOrderContract = iStockOrderContract;
            //默认值
            this.PageIndex = 1;
            this.PageSize = 20;
        }

        #endregion

        #region 属性

        #region 出库单据列表
        /// <summary>
        /// 单据类型列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Wrap<StockOutOrderInfo>> StockOutOrderList { get; set; }
        #endregion

        #region 关键字
        /// <summary>
        /// 关键字
        /// </summary>
        [DependencyProperty]
        public string Keywords { get; set; }
        #endregion

        #region 源储存类型列表
        /// <summary>
        /// 源储存类型列表
        /// </summary>
        [DependencyProperty]
        public IDictionary<string, string> SourceStorageLocationTypes { get; set; }
        #endregion

        #region 目标储存类型列表
        /// <summary>
        /// 目标储存类型列表
        /// </summary>
        [DependencyProperty]
        public IDictionary<string, string> TargetStorageLocationTypes { get; set; }
        #endregion

        #region 已选源储存类型
        /// <summary>
        /// 已选源储存类型
        /// </summary>
        [DependencyProperty]
        public SourceOrderType? SelectedSourceStorageLocationType { get; set; }
        #endregion

        #region 已选目标储存类型
        /// <summary>
        /// 已选目标储存类型
        /// </summary>
        [DependencyProperty]
        public SourceOrderType? SelectedTargetStorageLocationType { get; set; }
        #endregion

        #region 开始时间
        /// <summary>
        /// 开始时间
        /// </summary>
        [DependencyProperty]
        public DateTime? StartTime { get; set; }
        #endregion

        #region 结束时间
        /// <summary>
        /// 结束时间
        /// </summary>
        [DependencyProperty]
        public DateTime? EndTime { get; set; }
        #endregion

        #region 页码 —— int PageIndex
        /// <summary>
        /// 页码
        /// </summary>
        [DependencyProperty]
        public int PageIndex { get; set; }
        #endregion

        #region 页容量 —— int PageSize
        /// <summary>
        /// 页容量
        /// </summary>
        [DependencyProperty]
        public int PageSize { get; set; }
        #endregion

        #region 总记录数 —— int RowCount
        /// <summary>
        /// 总记录数
        /// </summary>
        [DependencyProperty]
        public int RowCount { get; set; }
        #endregion

        #region 总页数 —— int PageCount
        /// <summary>
        /// 总页数
        /// </summary>
        [DependencyProperty]
        public int PageCount { get; set; }
        #endregion

        #region 目标储存位置类型 —— StorageLocationType StorageLocationType
        /// <summary>
        /// 目标储存位置类型
        /// </summary>
        [DependencyProperty]
        public StorageLocationType? StorageLocationType { get; set; }
        #endregion

        #region 目标储存位置Id —— string StorageLocation
        /// <summary>
        /// 目标储存位置Id
        /// </summary>
        [DependencyProperty]
        public string StorageLocation { get; set; }
        #endregion


        #region 工厂模型Id —— Guid FacilityId
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        [DependencyProperty]
        public Guid? FacilityId { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load()
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(StorageLocationType? storageLocationType, string storageLocation, Guid? facilityId)
        {
            this.StorageLocationType = storageLocationType;
            this.StorageLocation = storageLocation;
            this.FacilityId = facilityId;
            this.SourceStorageLocationTypes = typeof(SourceOrderType).GetEnumMembers();
            this.TargetStorageLocationTypes = typeof(SourceOrderType).GetEnumMembers();
            await this.ReloadStockOutOrder();
        }
        #endregion

        #region 搜索
        /// <summary>
        /// 搜索
        /// </summary>
        public async void LoadStockOut()
        {
            await this.ReloadStockOutOrder();
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证
            StockOutOrderInfo[] checkedRoles = this.StockOutOrderList.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!checkedRoles.Any() || checkedRoles.Count() != 1)
            {
                MessageBox.Show("请勾选一条出库单据！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion

            this.Busy();
            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        //Private

        #region 加载出库单列表 —— async Task ReloadStockOutOrder()
        /// <summary>
        /// 加载出库单列表
        /// </summary>
        private async Task ReloadStockOutOrder()
        {
            this.Busy();

            PageModel<StockOutOrderInfo> StockOutOrders = await Task.Run(() => this._iStockOrderContract.Channel.GetStockOutOrdersByPage(
                 null,//关键字
                 null, //库存单据类Id
                 CheckStatus.Passed,//审核状态
                 null,//源单据类型
                 null, //源单据Id
                 null,//源储存位置类型
                 null, //源储存位置Id
                 StorageLocationType,//目标储存位置类型
                 StorageLocation,//目标储存位置Id
                 StartTime, //开始时间
                 EndTime,//结束时间
                 FacilityId,//工厂模型Id
                 1,//页码
                 int.MaxValue));
            this.RowCount = StockOutOrders.RowCount;
            this.PageCount = StockOutOrders.PageCount;
            this.StockOutOrderList = new ObservableCollection<Wrap<StockOutOrderInfo>>(StockOutOrders.Datas.Select(x => x.Wrap()));

            this.Idle();
        }
        #endregion
        #endregion
    }
}
